import com.alibaba.druid.pool.DruidDataSource;

import java.sql.SQLException;

public class Main {
    public static void main(String[] args) {//创建Druid数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("050214");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setMaxActive(10);// 设置最大连接数
        dataSource.setInitialSize(1); // 设置初始化连接数
        dataSource.setMaxWait(60000); // 设置最大等待时间
        dataSource.setMinIdle(1); // 设置最小连接数
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(true);
        dataSource.setTestWhileIdle(true);
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(300000);
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(1800);


        try {
            if (dataSource.getConnection() != null) {
                System.out.println("连接成功");
            } else {
                System.out.println("连接失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}